package com.lanlian.cayman.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.utils.NumberUtil2;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.MinXiAdapter;
import com.lanlian.cayman.bean.MinXiBean;

import java.util.ArrayList;

/**
 * 盘口明细控件
 */
public class MinXiView extends LinearLayout{
    MinXiAdapter mAdapter;
    private RecyclerView rv;
    Activity context;
    int gearCount=20;
    String last_price="1";
    ArrayList list=new ArrayList();
    public MinXiView(Context context) {
        super(context);
        initView();

    }
    public MinXiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=(Activity) context;
        initView();
    }
    private  void initView()
    {
        removeAllViews();
        LinearLayout ll=new LinearLayout(context);
        LayoutInflater inflater=context.getLayoutInflater();
        View view = inflater.inflate(R.layout.view_minxi, ll,false);
        rv= view.findViewById(R.id.recycler_view);
        addView(view);

        mAdapter=new MinXiAdapter(list);
        mAdapter.setLastDayPrice(last_price);
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(mAdapter);

    }
    public void reSet()
    {
        initView();
        list=new ArrayList();
    }
    /**
     * 设置上一个交易日收盘价格
     */
    public void setLastDayPrice(String price)
    {
        this.last_price=price;
        mAdapter.setLastDayPrice(last_price);
    }
    public void setData(MinXiBean mb)
    {
        if(NumberUtil2.isDaYu(mb.num,"0")<=0)
        {
            return;
        }
        setlist(mb);
        mAdapter.setData(list);
        if(getVisibility()==VISIBLE) {
            mAdapter.notifyDataSetChanged();
        }

    }
    //刷新数据
    public void refresh()
    {
        mAdapter.notifyDataSetChanged();
    }
    private void setlist(MinXiBean mb)
    {
        ArrayList list1=new ArrayList();
        list1.add(mb);
        if(list.size()<20)
        {
            list1.addAll(list);
        }
        else
        {
            list.remove(list.size()-1);
            list1.addAll(list);
        }
        list=list1;
    }

}
